package com.wanxin.wb.common.support;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.session.SqlSession;

/**
 * 
 * <B>Description:</B> <br>
 * <B>Create on:</B> 2015年5月31日 上午9:54:49<br>
 *
 * @param <T>
 * @author 
 * @version 1.0
 */
public class CommodityMapperProxyFactory<T> {

    private final Class<T>            mapperInterface;
    private Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();

    public CommodityMapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public Class<T> getMapperInterface() {
        return mapperInterface;
    }

    public Map<Method, MapperMethod> getMethodCache() {
        return methodCache;
    }

    @SuppressWarnings("unchecked")
    protected T newInstance(CommodityMapperProxy<T> mapperProxy) {
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(),
            new Class[] { mapperInterface }, mapperProxy);
    }

    public T newInstance(SqlSession sqlSession) {
        final CommodityMapperProxy<T> mapperProxy = new CommodityMapperProxy<T>(sqlSession,
            mapperInterface, methodCache);
        return newInstance(mapperProxy);
    }

}
